import os, os.path, sys, wx
from wx import xrc

#import matplotlib
#matplotlib.use('WXAgg')
#import matplotlib.pyplot as plt
#from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
#import pylab

from SVMs import SVMs
from DataDisplayer import DataDisplayer
import pdb

########################################
###   Figure out the app directory and
###   set up the layoutxrc global

APPDIR = sys.path[0]
if os.path.isfile(APPDIR):  #py2exe/py2app
  APPDIR = os.path.dirname(APPDIR)
layoutxrc = None

######################################
###   The main frame of the app

class MainFrame:
  def __init__(self):
    '''Constructor'''
    global layoutxrc
    #layoutxrc = xrc.XmlResource(os.path.join(APPDIR, 'layout.xrc'))
    layoutxrc = xrc.XmlResource('layout.xrc')

    self.frame = layoutxrc.LoadFrame(None, 'mainframe')
    self.frame.SetBackgroundColour(wx.NamedColor('WHITE'))
    self.dataDisplayer = DataDisplayer(self.frame)
    #pdb.set_trace()
    self.SVMs = SVMs(self.dataDisplayer)

    self.initUI()
    self.bindEvents()

    self.SVMs.initUI()
    self.SVMs.bindEvents()

    self.frame.Show()

  def initUI(self):
    pass

  def bindEvents(self):
      pass



  def onIdle(self, event):
    '''Responds to idle time in the system'''
    # when the timer says it's time, we do the actual downloading in the main thread (wx doesn't work well in secondary threads)


  def onClose(self, event):
    '''Closes the application'''
    dlg = wx.MessageDialog(self.frame, "Exit the program?", "Exit", wx.YES_NO | wx.ICON_QUESTION)
    if dlg.ShowModal() == wx.ID_YES:
      self.frame.Destroy()  # frame
    dlg.Destroy()


  def onExit(self, event):
    '''Exit the app'''
    self.Close(True)


  def onRefresh(self, event):
    '''Handles the refresh button'''
    print "I should be refreshing the picture now, but the app logic is omitted in this tutorial"
